<?php

namespace App\Tools;

use Ixudra\Curl\Facades\Curl;

class Oppo
{
    protected $oppoUrl = 'https://api.ads.heytapmobi.com/api/uploadActiveData';
    protected $oppoSalt = 'e0u6fnlag06lc3pl';
    protected $oppoBase64Key = 'XGAXicVG5GMBsx5bueOe4w==';
    protected $oppoPkg = 'com.jxfq.dalle';
    protected static $dataTypeMsg = [
        1=>'active',
        2=>'register',
        3=>'pay',
    ];

    public function sendData($imei = '',$ouId = '' , $mac = '' , $clientIp = '' , $dataType = '' , $payAmount = '',$ascribeType = 0 ,$adid = '')
    {
        if(!($imei || $ouId)){
            return false;
        }
//        if ($ascribeType = 0){
//            return false;
//        }
        //$dataTypeMsg = $dataType && self::$dataTypeMsg[$dataType]?self::$dataTypeMsg[$dataType]:'null';
        $postData = [];

        //处理数据
//        if ($dataType == 3){
//            $postData['payAmount'] = $payAmount?intval($payAmount * 100):'';    //付费金额（单位：分）
//        }
        $postData['appType'] = 1;                   // 1 应用 2 游戏 3 快应用 0 其他，默认 1 应用
        $postData['clientIp'] = $clientIp?:'';      // ip
        $postData['dataType'] = $dataType?:'';      //1、激活，2、注册，3、游戏付费，
        $postData['ascribeType'] = $ascribeType;               //1：广告主归因，0：OPPO归因（默认或者不填即为 0），2：助攻归因
        $postData['adid'] = $adid;               //广告主归因id
        $postData['channel'] = 1;                   //1、OPPO，2、一加，0、其他
        $postData['imei'] = $imei?$this->encrypt(md5($imei),$this->oppoBase64Key):'';
        $postData['ouId'] = $ouId?$this->encrypt($ouId,$this->oppoBase64Key):'';
        $postData['type'] = 0;                     //Imei 原始加密类型1： md5 加密 0： 无加密（默认为0）如果传 oaid，type 值填 0
        $postData['pkg'] = $this->oppoPkg;         //包名，
        $postData['mac'] = $mac?:'';
        $postData['timestamp'] = $this->getMsecTime();
        if ($imei){
            unset($postData['ouId']);
            $postData['type'] = 1;
        }else{
            unset($postData['imei']);
        }
        //设置请求头
        $header = $this->setHeader($postData);
        //上报统计
        return Curl::to($this->oppoUrl)->withData($postData)->withHeaders($header)->asJsonRequest()->post();
    }
    public function setHeader($postData){
        $data = [];
        $time = (string) $this->getMsecTime();
        //生成签名
        $data[] = 'signature:'.$this->createSign($postData,$time,$this->oppoSalt);
        $data[] = 'timestamp:'.$time;
        $data[] = 'Content-Type:application/json';

        return $data;
    }
    protected function createSign($postData,$time,$salt){
        $data = json_encode($postData).$time.$salt;
        return md5($data);
    }

    /**
     * imei 与 mac 字段 AES 算法进行加密
     * @param $input
     * @param $base64Key
     * @return false|string
     */
    public function encrypt($input, $base64Key){
        return openssl_encrypt($input, 'AES-128-ECB',base64_decode($base64Key),0,'');
    }

    /**
     * 获取毫秒级别的时间戳
     */
    public function getMsecTime()
    {
        list($msec, $sec) = explode(' ', microtime());
        $msectime =  (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
        return $msectime;
    }
}